Supported with v3.0.0+
The Dice Roller Widget is a widget you can add to your game to easily add dice rolling support, from the middle of the screen or from the user's drawer position.
This widget assumes the project has the standard gravity set within your project.
For any user support, the widget assumes the camera is set in a way where the edges of the dice roller match up with the edges of the screen to allow the dice rolls to come from the user's drawer location.
You can add the Dice Roller Widget through the Gameboard menu options at Gameboard > Tool > Add Dice Roller Widget
*WARNING* This will modify your camera settings to line up with the widget. If you don't want this to happen, the prefab can be found in Package > Gameboard Unity Plugin > Runtime > Tools > Prefabs > DiceRoller.prefab
This will add the gameboard SDK (if it doesn't already exist) and align the camera to support the user dice rolling, so the dice would roll from the user's drawer location.
Now that you have the prefab pulled into your project, you're presented with a few options to customize the dice widget.
RollDice
method is called. If a camera is supplied, the dice will be rolled in the middle of the camera view from Spawn Distance from Camera
away from the camera.RollDiceForUser
method or if the Touch To Move Dice
option is true.Now that you have your dice roller configured, you will want to start rolling dice. For all of the methods the allowing rolling and modifying the dice, there are two events that will be important to listen to:
void Start()
{
roller.DiceRollCompleted += RollComplete;
roller.DiceRollTotalChanged += RollValueChanged;
}
void RollComplete(int result)
{
// result after roll
}
void RollValueChanged(int result)
{
// result after any change (roll, clear dice, etc)
}
There are a few methods available that you can use to roll dice depending on how you have configured your dice roller.
RollDice(int[] diceSizes)
DiceRollCompleted
or DiceRollTotalChanged
//Rolls a d4, two d6s, a d8, d10, and d20
roller.RollDice(new int[7] { 4, 6, 6, 8, 10, 12, 20 });
RollDiceForUser(int[] diceSizes, string userId)
DiceRollCompleted
or DiceRollTotalChanged
. They return the total of all the dice on the board, to get the total for the specific user, you can look at the CurrentTotalForUser dictionary with CurrentTotalForUser[{userId}]
to get the total for that user. Or if you want to see the individual dice for that user, you can look at the CurrentDiceForUser dictionary.// Rolls two d6s for the user of that userPresence object
roller.RollDiceForUser(new int[2] { 6, 6 }, userPresence.userId);
ReRollCurrentDice()
roller.ReRollCurrentDice();
ClearDice()
roller.ClearDice();
ClearDiceForUser(string userId)
roller.ClearDiceForUser(userPresence.userId);
The Dice Roller will roll GameboardDice
. You can create your own dice if they inherit from the GameboardDice
object. GameboardDice
will have the following usefil properties:
There are also some useful methods that can be used on the GameboardDice
object:
int
indicating the current side that is facing upwards.Each GameboardDice
also has it's own event called RollCompleted
that you can listen to if you want to perform actions based on specific die rolls. That event returns the GameboardDice
object that was rolled, and the int result of that roll.
This section include the entire code in one single, easy to copy section.
public GameboardDiceRoller roller;
private UserPresenceController userPresenceController;
void Start()
{
GameObject gameboardObject = GameObject.FindWithTag("Gameboard");
roller.DiceRollCompleted += RollComplete;
roller.DiceRollTotalChanged += RollValueChanged;
userPresenceController = gameboardObject.GetComponent<UserPresenceController>();
userPresenceController.OnUserPresence += OnUserPresence;
}
void RollComplete(int result)
{
// result after roll
}
void RollValueChanged(int result)
{
// result after any change (roll, clear dice, etc)
}
public void RollDice()
{
roller.RollDice(new int[7] { 4, 6, 6, 8, 10, 12, 20 });
}
public void ReRollCurrentDice()
{
roller.ReRollCurrentDice();
}
public void ClearDice()
{
roller.ClearDice();
}
private void OnUserPresence(GameboardUserPresenceEventArgs userPresence)
{
switch (userPresence.changeValue)
{
case DataTypes.UserPresenceChangeTypes.REMOVE:
roller.ClearDiceForUser(userPresence.userId);
break;
case DataTypes.UserPresenceChangeTypes.CHANGE_POSITION:
case DataTypes.UserPresenceChangeTypes.CHANGE:
roller.RollDiceForUser(new int[2] { 6, 6 }, userPresence.userId);
break;
}
}